package handlers

import (
	"github.com/gin-gonic/gin"
	"life-share/models"
)

func CreateEssay(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("ing")
	if title == "" || content == "" || img == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.Essay{
		Title:   title,
		Content: content,
		Img:     img,
	}
	err := models.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ListEssay(c *gin.Context) {
	var essay []models.Essay
	err := models.DB.Debug().Find(&essay).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "列表失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code":  200,
		"essay": essay,
		"msg":   "列表成功",
	})
	return
}
